﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Course.Entity;

public partial class sysadmin_CourseManager_AddCourseResource : PowerCreator.LMS.Web.Base.BasePage
{
    Course opCourse = new Course();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            BinderResourceType();
            int courseid = 0;
            int resourceid = 0;
            if (Request.QueryString["CourseID"] != null)
            {
                courseid = int.Parse(Request.QueryString["CourseID"].ToString());
                ViewState["CourseID"] = courseid.ToString();
                resourceid = int.Parse(Request.QueryString["ResourceID"].ToString());
                ViewState["ResourceID"] = resourceid.ToString();
                BinderForm();
            }
        }
    }
    protected override void InitPage()
    {
        lbl_InfoTitle_AddCourseResource.Text = GetLanageValueByKey("AddCoursePaper_msg_m6", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Course.Text = GetLanageValueByKey("ResourceSelect_msg_m2", PowerCreator.LMS.Language.LanagetItemType.Page) + "：";
        lbl_Resource.Text = GetLanageValueByKey("AddCoursePaper_msg_m7", PowerCreator.LMS.Language.LanagetItemType.Page) + "：";
        lbl_ResourceType.Text = GetLanageValueByKey("AddCoursePaper_msg_m3", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private void BinderResourceType()
    {
        DataTable dt = new DataTable();
        dt = opCourse.QueryResourceType("");
        ddl_CoursersRestpID.DataSource = dt;
        ddl_CoursersRestpID.DataTextField = "RestpName";
        ddl_CoursersRestpID.DataValueField = "RestpID";
        ddl_CoursersRestpID.DataBind();
    }
    private void BinderForm()
    {

        VOTResource vor = new VOTResource();
        VOTOCourse voc = new VOTOCourse();
        vor = opCourse.LoadResource(int.Parse(ViewState["ResourceID"].ToString()));
        voc = opCourse.LoadVOTCourse(int.Parse(ViewState["CourseID"].ToString()));
        lbl_CourseName.Text = voc.CourseName;
        lbl_ResourceName.Text = vor.ResourceName;
    }
    private bool CheckPageData()
    {
        string searcht = string.Format("FKCourseresCourseID={0} and FKCoursersResourceID={1} ", ViewState["CourseID"], ViewState["ResourceID"]);
        if (opCourse.QueryCourseResourceCount(searcht) > 0)
        {
            //lbl_msg_Error.Text = GetLanageValueByKey("lbl_msg_addjob_aspx_count", PowerCreator.LMS.Language.LanagetItemType.MSG);
            lbl_msg_Error.Text = "<br />" + GetLanageValueByKey("AddCoursePaper_msg_m4", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        
        VOTResourceType voResourceType = new VOTResourceType();
        voResourceType.RestpName = ddl_CoursersRestpID.SelectedItem.Text;
        voResourceType.RestpID = int.Parse(ddl_CoursersRestpID.SelectedItem.Value);

        PowerCreator.LMS.Web.CreatedByInfo cinfo = GetCreatedByInfo();

        VOTOCourse voCoures = opCourse.LoadVOTCourse(int.Parse(ViewState["CourseID"].ToString()));
        CourseresourceResourceInfo voResourceInfo = new CourseresourceResourceInfo();
        voResourceInfo.CoursersIndex = 0;
        
        VOTResource voResource = opCourse.LoadResource(int.Parse(ViewState["ResourceID"].ToString()));
        
        voResourceInfo.ResourceID = voResource.ResourceID;
        voResourceInfo.ResourceName = voResource.ResourceName;

        if (opCourse.AddCourseResource(null, voResourceType, cinfo, voCoures,voResourceInfo, null, CoursersClass.Courseware))
        {

            string url = "ResourceList.aspx?CourseID=" + ViewState["CourseID"].ToString();
            Script.RedirectURL(url);
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "ResourceList.aspx?CourseID=" + ViewState["CourseID"].ToString();
        Script.RedirectURL(url);
    }

}
